#!/usr/bin/php
<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

require("bashcommon.php");
$logid = startlog("cleanup");
$blog =& LoggerManager::getLogger('bash_cleanup');
$blog->info("Started");
$_log->info("Started");

$blog->debug("==== UPLOADS ====");

$upload = new upload;
$orphans = $loader->loadListBy($upload,$upload->listOrphans());

$blog->info("ORPHANS: ".count($orphans));
$_log->info("ORPHANS: ".count($orphans));

foreach ($orphans as $u) {
	$loader->delete($u);
	
	$updata = new upload_data;
	$updata->objid=$u->id;
	$loader->delete($updata);
	
	$filename = UPLOAD_PATH."/".$u->id;
	unlink($filename);
	deltree(TEMP_PATH."/".$u->id);
}

$expired = $loader->loadListBy($upload,$upload->listExpired());

$blog->info("EXPIRED: ".count($expired));
$_log->info("EXPIRED: ".count($expired));

foreach ($expired as $u) {
	$updata = new upload_data;
	$updata->objid=$u->id;
	$dlist = $loader->loadList($updata);

	foreach ($dlist as $ud) {
		$file = new files($u->projectid);
		$file->id=$ud->fileid;
		$ret = $loader->get($file);
		if ($ret) {
			if ($file->statusid = FILE_STATUS_CMPND) {
				switch ($ud->filestatus) {
					case FILE_STATUS_PROOF:
						$file->statusid = FILE_STATUS_CHKPND;
						break;
					default:
						$file->statusid = FILE_STATUS_LOCKED;
						break;
				}

				$file->stamp=util::hashcode($file->id);
				$loader->update($file);

				$his = new history($u->projectid);
				$his->statusid=$file->statusid;
				$his->fileid=$file->id;
				$his->userid=BOT_USER;
				$his->comment= "Upload rejected. Older than ".UPLOAD_EXPIRE." days";
				$loader->save($his);
			}
		}
	}
	
	$loader->delete($u);
	$loader->delete($updata);
	$filename = UPLOAD_PATH."/".$u->id;
	unlink($filename);
	deltree(TEMP_PATH."/".$u->id);
}

$blog->debug("==== CLEAN TEMP DB ====");

$t = new temporal;
$a = $loader->execute($t->cleanUp());
$blog->info("Removed: $a");
$_log->info("DB Removed: $a");

$blog->debug("==== CLEAN TEMP FOLDER ====");
$blog->debug(TEMP_PATH);

$a = deltreeatime(TEMP_PATH,TMP_CLEANUP);
$blog->info("Removed: $a");
$_log->info("TMP Removed: $a");

$blog->debug("==== CLEAN UPLOADS FOLDER ====");
$blog->debug(UPLOAD_PATH);

$a = deltreeatime(UPLOAD_PATH,UPLOAD_EXPIRE);
$blog->info("Removed: $a");
$_log->info("UPLOAD Removed: $a");

$blog->info("Finished.");
$_log->info("Finished.");
endlog($logid);
$db->close();

?>